package com.dzsw.camunda.interceptor;

import com.dzsw.camunda.utils.IpUtil;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

public class WhiteIpInterceptor implements HandlerInterceptor {

    private List<String> whiteIpList;

    public WhiteIpInterceptor(List<String> whiteIpList) {
        this.whiteIpList = whiteIpList;
    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String ip = IpUtil.getIpAddr(request);
        if (whiteIpList.contains(ip)) {
            return true;
        } else {
            throw new Exception("非法IP");
        }
    }
}
